function q_complete= complete_information...
    (v_draws, u_draw, w_draw, beta, lambda, I) 

% Step 1: determine Optimal total Q
% suppose all firms produce. then the foc is 
% P(Q) -beta q_i = (v_i+w) + lambda q_i
% add FOC for all firms 
% I* P(Q) -(beta+\lambda) Q = (sum v_i^*) 
% dividing by I 
% P(Q) -(beta+lambda) Q/I = v_avg
% substituting P(Q) = u -beta Q above we get 
% u -beta Q^*-(beta+lambda)Q^*/I = v_avg^*
% beta Q^* +(beta+lambda)Q*/I=u-v_avg^*
%[beta + (beta+lambda)/I]* Q^* = (u-v_avg^*)
% Q^* = (u-v_avg^*)./[beta + (beta+lambda)/I]--> total output. 

%Step 2: from Q^* determine q_i
% foc of a firm i with marginal cost = v_i^* + lambda q_i is 
% d/dq[P(q_i + Q_{-i})q_i -(v_i^* q_i + lambda/2 q_i^2)]=0
% P(Q^*) + P'(q_i + Q_{-i}) q_i -(v_i^* + lambda q_i)=0
% P(Q^*) - beta q_i -v_i^* - lambda q_i =0
% q_i^* = [P(Q^*) - v_i^*]/(beta + lambda)

% Step 3. check if all q_i^* are non-negative. If they are, stop. 

% Step 4. if any one is negative, remove the firm with highest v_i. then
% repeat 1-3 for the subset. and set q_i=0 for that excluded firm. and so
% on. 

%% 
% step 0 rank: 
v_draws = v_draws+w_draw; % v_i^*
[v_draws_new, efficiency] = sort(v_draws);
q_i_star =zeros(20,1);
for ii=20:-1:1
    %1. Step 1
    v_avg  = sum(v_draws_new(1:ii))/ii;
    Q_star = (u_draw-v_avg)./(beta + (beta+lambda)/ii);
    P_star = u_draw -beta *Q_star;

    %2. Step 2.
    temp = (P_star - v_draws_new)./(beta + lambda); % qt. for firms

    % 3. check for positive output 
    if sum(temp>0)==ii
        q_i_star(1:ii,1) = temp;
        break
    else 
        v_draws_new=v_draws_new(1:ii-1); 
    end
end

% to assign the firm id to the quantities we need to recover firm's groupid
q_complete = zeros(I,1); 

for kk=1:I
    if ismember(kk,efficiency(1:ii))==1 % firm kk is among those >0 qt. 
        loc = find(efficiency==kk); 
        q_complete(kk,1)= q_i_star(loc); 
    end
end


end
